﻿using System;
using FineUI;
using System.Collections.Generic;
using SDLITS.Common;
using SDLITS.Model;

namespace SDLITS.Web.Customer
{
    public partial class RentExpensePayManage : CommonBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btn_jf.Hidden = true;
                btn_tz.Hidden = true;
                BindCom();
            }
        }
        protected void BindCom()
        {
            List<V_drpCompany> coms = SDDbSession.Default.From<V_drpCompany>().ToList();
            drp_com.DataSource = coms;
            drp_com.DataTextField = "Value";
            drp_com.DataValueField = "ID";
            drp_com.DataBind();
            drp_com.Items.Insert(0, new ListItem("--选择公司--", "-1"));
        }
        protected void BindCon(string comid)
        {
            List<Contract> coms = SDDbSession.Default.From<Contract>().Where(Contract._.CompanyID == comid).ToList();
            drp_con.DataSource = coms;
            drp_con.DataTextField = "ContractNo";
            drp_con.DataValueField = "ContractID";
            drp_con.DataBind();
            drp_con.Items.Insert(0, new ListItem("--选择合同--", "-1"));
        }
        protected void drp_com_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drp_com.SelectedValue != "-1")
            {
                string id = drp_com.SelectedValue;
                BindCon(id);
                Contract Tm = null;
                if (SDDbSession.Default.From<Contract>().Where(Contract._.CompanyID == id && Contract._.StatusID == 1).Count() > 0)
                {
                    Tm = SDDbSession.Default.From<Contract>().Where(Contract._.CompanyID == id && Contract._.StatusID == 1).OrderBy(Contract._.CreateDate.Desc).ToFirst();
                }
                else
                {
                    Tm = SDDbSession.Default.From<Contract>().Where(Contract._.CompanyID == id).OrderBy(Contract._.CreateDate.Desc).ToFirst();
                }
                BindData(Tm);
            }
        }

        protected void drp_con_SelectedIndexChanged(object sender, EventArgs e)
        {
            Contract Tm = SDDbSession.Default.From<Contract>().Where(Contract._.ContractID == drp_con.SelectedValue).ToFirst();
            BindData(Tm);
        }

        protected void btn_jf_Click(object sender, EventArgs e)
        {
            UpdateBata("缴费");
        }

        protected void btn_tz_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_tzrq.Text))
            {
                Alert.Show("请选择退租日期");
                return;
            }
            UpdateBata("退租");
        }
        protected void UpdateBata(string str)
        {
            int result = 0;
            DateTime? dt = null;
            if (!string.IsNullOrEmpty(txt_tzrq.Text))
            {
                dt = DateTime.Parse(txt_tzrq.Text);
            }
            if (string.IsNullOrEmpty(txt_sjzj.Text) || txt_sjzj.Text.Equals("0"))
            {
                txt_sjzj.Text = txt_rent.Text;
            }
            if (string.IsNullOrEmpty(txt_sjglf.Text) || txt_sjglf.Text.Equals("0"))
            {
                txt_sjglf.Text = txt_rentmanage.Text;
            }
            string rent = "0", rentManage = "0", rentDefault = "0", rentActual = "0", rentManageActual = "0";
            if (!string.IsNullOrEmpty(txt_rent.Text.Trim()))
            {
                rent = txt_rent.Text;
            }
            if (!string.IsNullOrEmpty(txt_sjzj.Text.Trim()))
            {
                rentActual = txt_sjzj.Text;
            }
            if (!string.IsNullOrEmpty(txt_rentmanage.Text.Trim()))
            {
                rentManage = txt_rentmanage.Text;
            }
            if (!string.IsNullOrEmpty(txt_sjglf.Text.Trim()))
            {
                rentManageActual = txt_sjglf.Text;
            }
            if (!string.IsNullOrEmpty(txt_wyj.Text.Trim()))
            {
                rentDefault = txt_wyj.Text;
            }
            if (!string.IsNullOrEmpty(drp_com.SelectedValue))
            {
                Contract tm = new Contract();
                tm.ContractID = int.Parse(hidcontractid.Text);
                tm.Attach();
                tm.Rent = rent;
                tm.RentActual = rentActual;
                tm.RentManage = rentManage;
                tm.RentManageActual = rentManageActual;
                tm.RentDefault = rentDefault;
                tm.RentEndDate = dt;
                tm.IsFeeDone = true;
                result = SDDbSession.Default.Update<Contract>(tm);

                SDDbSession.Default.Update<Company>(Company._.StatusID, 1, Company._.CompanyID == drp_com.SelectedValue);
                if (result > 0)
                {
                    Alert.Show(str + "成功");
                    Contract Tm = SDDbSession.Default.From<Contract>().Where(Contract._.ContractID == drp_con.SelectedValue).ToFirst();
                    BindData(Tm);
                }
            }
            else
            {
                Alert.Show("请选择需要缴费的公司");
            }
        }
        protected void BindData(Contract Tm)
        {
            if (Tm != null)
            {
                hidcontractid.Text = Tm.ContractID.ToString();
                txt_ContractNO.Text = Tm.ContractNo;
                txt_status.Text = string.IsNullOrEmpty(Tm.IsFeeDone.ToString()) ? "" : (Boolean.Parse(Tm.IsFeeDone.ToString()) ? "已缴费" : "未缴费");
                txt_rent.Text = Tm.Rent;
                txt_start.SelectedDate = Tm.StartDate;
                txt_end.SelectedDate = Tm.EndDate;
                txt_rentmanage.Text = Tm.RentManage;
                txt_wyj.Text = Tm.RentDefault;

                txt_tzrq.SelectedDate = Tm.RentEndDate;
                txt_sjzj.Text = Tm.RentActual;
                txt_sjglf.Text = Tm.RentManageActual;

                if (!string.IsNullOrEmpty(txt_tzrq.Text))
                {
                    txt_status.Text = "已退费";
                }
                if (string.IsNullOrEmpty(Tm.RentActual))
                {
                    txt_sjzj.Text = txt_rent.Text;
                }
                if (string.IsNullOrEmpty(Tm.RentManageActual))
                {
                    txt_sjglf.Text = txt_rentmanage.Text;
                }
                try
                {
                    txt_tzj.Text = (Convert.ToDecimal(txt_rent.Text) - Convert.ToDecimal(txt_sjzj.Text)).ToString("0.00");
                }
                catch (Exception)
                {
                    txt_tzj.Text = "0";
                }
                try
                {
                    txt_tglf.Text = (Convert.ToDecimal(txt_rentmanage.Text) - Convert.ToDecimal(txt_sjglf.Text)).ToString("0.00");
                }
                catch (Exception)
                {
                    txt_tglf.Text = "0";
                }

                if (txt_status.Text == "已缴费")
                {
                    btn_tz.Hidden = false;
                    Form2.Hidden = false;
                    btn_jf.Hidden = true;
                }
                else if (txt_status.Text == "未缴费")
                {
                    btn_tz.Hidden = true;
                    Form2.Hidden = true;
                    btn_jf.Hidden = false;
                }
                else if (txt_status.Text == "已退费")
                {
                    btn_jf.Hidden = true;
                    btn_tz.Hidden = true;
                    Form2.Hidden = true;
                }
            }
        }
    }
}